<?php
if (!defined('INDEX') || INDEX == false) die ('Acc�s denied');

/**
 * Library manager
 *
 * @version 0.1
 * @copyright 2010
 */

/**
 * Manager for libraries
 *
 */
class core_libraries
{
	/**
	* Statics proprieties
	*/
	private static $libraries = array();

	/**
	* Statics methods
	*/
	public static function IsLoaded($Job)
	{
		return isset(self::$libraries[$Job]);
	}
	
	public static function LoadLibrary($Job, $Library = null)
	{
		if ($Library !== null && self::$libraries[$Job] != $Library)
		{
			throw new LibraryLoadingError('You try to load ' . $Job . ' with the lib ' . $Library . ' but it is already loaded with ' . self::$libraries[$Job]);
		}
		elseif (isset(self::$libraries[$Job]))
		{
			return true;
		}
		elseif ($Library === null)
		{
			$jobs = new SQL('jobs');
			$jobs = $jobs->where(array('Jname' => $Job))->select('Jname');
			if ($jobs->count() < 1)
			{
				throw new LibraryLoadingError('Job not found. Have you installed libs for ' . $Job . ' ?');
			}
			else
			{
				$lib = $jobs->current();
				require_once LIBSDIR . $lib->Jlib . DIRECTORY_SEPARATOR . $Job . '.php';
			}
			self::$libraries[$Job] = $lib->Jlib;
			return true;
		}
		else
		{
			$file = LIBSDIR . $Library . DIRECTORY_SEPARATOR . $Job . '.php';
			if (file_exists($file))
			{
				require_once $file;
				return true;
			}
			else
				throw new LibraryLoadingError('Library ' . $Library . ' not found for the job ' . $Job);
		}
	}
}
?>